﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlickrNet;
using System.Windows.Forms;

namespace Artshare
{
    class ApiMethods
    {
        private Flickr flickr;
        private String apiKey = "dd57123bcd76f8b963f67a20b1ba34ed";//Properties.Settings.Default.Apikey;
        private String sharedSecret= "05125b325cc02477";//Properties.Settings.Default.sharedSecret;
        private String token
            {get{return Properties.Settings.Default.Token;}
             set{Properties.Settings.Default.Token = value;
             Properties.Settings.Default.Save();}
             }
        private Auth auth;
        private String frob;
        public ApiMethods()
        {
            flickr = new Flickr(apiKey, sharedSecret);

        }

        public String login()
        {
            return null;
        }
        
        public Boolean hasValidToken()/*check if a token i valid*/
        {                                          
            try
            {
                auth = flickr.AuthCheckToken(token);
                flickr.AuthToken = auth.Token;
                Console.WriteLine("token: " + flickr.AuthToken);
                return true;
            }
            catch (FlickrApiException fae)
            {

                MessageBox.Show(fae.Message);
                return false;
            }
        }
        
        public void authApp()
        {

            frob = flickr.AuthGetFrob();
            string authUrl = flickr.AuthCalcUrl(frob, AuthLevel.Delete);
            System.Diagnostics.Process.Start(authUrl);
         }

        public void getToken()
        {
            Auth au = flickr.AuthGetToken(frob);
            String t = au.Token;
            token = t;
            hasValidToken();
         }

        public void searchByTag(String tag)
        {
            PhotoSearchOptions pso = new PhotoSearchOptions();
            pso.UserId = auth.User.UserId;
            pso.Tags = tag;
            PhotoCollection phc = flickr.PhotosSearch(pso);
            foreach(Photo p in phc){
                Console.WriteLine(p.Title);
            }
        }
    }
}
